<template>
  <div class="">
    <BasicCard style="margin-top: 20px">
      <BasicVxeTable v-bind="gridOptions"> </BasicVxeTable>
    </BasicCard>
    <BasicCard style="margin-top: 20px">
      <BasicVxeTable v-bind="gridOptionsNight"> </BasicVxeTable>
    </BasicCard>
  </div>
</template>

<script setup lang="ts">
  import { columns } from './data'
  import { splitTsIdToTime } from '@/utils'
  import { getPingZhongRiBao_xs } from '@/api/shuJuBaoBiao'

  const { t } = useI18n()

  const gridOptions = reactive<BasicGridProps>({
    extraConfig: {
      title: t('custom.班产日报表-早班'),
      isCustom: false,
      isExport: true,
      isFilterAndSort: true
    }, // 拓展的配置
    id: `shujubaobiao-pingzhongfenleiribao`, // id用于缓存,务必配置
    maxHeight: 500,
    loading: true,
    showFooter: true,
    columns,
    toolbarConfig: {}, // 工具栏
    exportConfig: {
      filename: t('custom.班产日报表-早班')
    },
    footerMethod({ columns, data }) {
      return footerMothodFunc(columns, data)
    }
  })

  const gridOptionsNight = reactive<BasicGridProps>({
    extraConfig: {
      title: t('custom.班产日报表-晚班'),
      isCustom: false,
      isExport: true,
      isFilterAndSort: true
    }, // 拓展的配置
    id: `shujubaobiao-pingzhongfenleiribao-night`, // id用于缓存,务必配置
    maxHeight: 500,
    loading: true,
    showFooter: true,
    columns,
    toolbarConfig: {}, // 工具栏
    exportConfig: {
      filename: t('custom.班产日报表-晚班')
    },
    footerMethod({ columns, data }) {
      return footerMothodFunc(columns, data)
    }
  })

  const getList = () => {
    getPingZhongRiBao_xs({
      TsIdStart: 25072101,
      TsIdEnd: 25072103
    })
      .then((res: any) => {
        gridOptions.loading = false
        gridOptionsNight.loading = false

        gridOptions.data = res.byTsidMaterialDetail
          .filter((value) => {
            return value.shiftName == '早'
          })
          .map((value) => {
            return {
              ...value,
              tsid: splitTsIdToTime(value.tsid) + value.shiftName
            }
          })
        gridOptionsNight.data = res.byTsidMaterialDetail
          .filter((value) => {
            return value.shiftName == '晚'
          })
          .map((value) => {
            return {
              ...value,
              tsid: splitTsIdToTime(value.tsid) + value.shiftName
            }
          })
      })
      .catch(() => {
        gridOptions.loading = false
        gridOptionsNight.loading = false
      })
  }

  const footerMothodFunc = (columns, data) => {
    return [
      columns.map((column, columnIndex) => {
        if (columnIndex === 0) {
          return '合计'
        } else if (columnIndex === 4) {
          return handleFunc(data, 'total', 'machineCnt')
        } else if (columnIndex === 5) {
          return handleFunc(data, 'total', 'dofferCnt')
        } else if (columnIndex === 6) {
          return handleFunc(data, 'average', 'dofferTimeAvg', 60)
        } else if (columnIndex === 7) {
          return handleFunc(data, 'average', 'efcRun')
        } else if (columnIndex === 8) {
          return handleFunc(data, 'total', 'theoreticalYarnWeight')
        } else {
          return '--'
        }
      })
    ]
  }

  const handleFunc = (data, type, key, divisor = 1) => {
    let count = 0
    data.forEach((item) => {
      count += item[key] / divisor
    })
    return type == 'total' ? count.toFixed(2) : (count / data.length).toFixed(1)
  }

  onMounted(() => {
    getList()
  })
</script>
